package com.hd.crdao.adapter;

import android.support.v7.widget.CardView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hd.crdao.R;
import com.hd.crdao.base.MyBaseAdapter;
import com.hd.crdao.bean.MyBankCardBean;
import com.hd.crdao.utils.UIUtils;

import java.util.List;

/**
 *
 *@author Programming
 *@date 2017/5/26 0026
 */
public class MyBankCardAdapter extends MyBaseAdapter<MyBankCardBean.RowsBean> {

    private List<String> banklists;

    public MyBankCardAdapter(List<MyBankCardBean.RowsBean> dataSets, List<String> banklists) {
        super(dataSets);
        this.banklists = banklists;

    }

    private int[] backgrounds = new int[]{
            R.drawable.bank_bg_nongye_3x,
            R.drawable.bank_bg_jiaotong_3x,
            R.drawable.bank_bg_zhongguo_3x,
            R.drawable.bank_bg_jianshe_3x,
            R.drawable.bank_bg_guangda_3x,
            R.drawable.bank_bg_xingye_3x,
            R.drawable.bank_bg_zhaoshang_3x,
            R.drawable.bank_bg_minsheng_3x,
            R.drawable.bank_bg_gongshang_3x,
            R.drawable.bank_bg_pufa_3x,
            R.drawable.bank_bg_pingan_3x,
            R.drawable.bank_bg_zhongxin_3x,
            R.drawable.bank_bg_huaxia_3x,
            R.drawable.bank_bg_youzheng_3x,
            R.drawable.bank_bg_xinyongshe_3x,
            R.drawable.bank_bg_moren_3x

    };

    private int[] images = new int[]{
            R.drawable.bank_nongye_2x,  //农业银行
            R.drawable.bank_jiaotong_2x,//交通银行
            R.drawable.bank_zhongguo_2x,
            R.drawable.bank_jianshe_2x,
            R.drawable.bank_guangda_2x,
            R.drawable.bank_xingye_2x,
            R.drawable.bank_zhaoshang_2x,
            R.drawable.bank_minsheng_2x,
            R.drawable.bank_gongshang_2x,
            R.drawable.bank_pufa_2x,
            R.drawable.bank_pingan_2x,
            R.drawable.bank_zhongxin_2x,
            R.drawable.bank_huaxia_2x,
            R.drawable.bank_youzheng_2x,
            R.drawable.bank_xinyongshe_2x,
            R.drawable.bank_moren_2x

    };

    private int[] backgroundColor = new int[]{
            R.color.nonghang,
            R.color.jiaotong,
            R.color.zhongguo,
            R.color.jianshe,
            R.color.guangda,
            R.color.xingye,
            R.color.zhaoshang,
            R.color.minsheng,
            R.color.gongshang,
            R.color.pufa,
            R.color.pingan,
            R.color.zhongxin,
            R.color.huaxia,
            R.color.youzheng,
            R.color.xinyongshe,
            R.color.moren
    };


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(UIUtils.getContext(), R.layout.item_mybankcard, null);

            holder.mCardview = (CardView) convertView.findViewById(R.id.cardview);
            holder.mBankcardNumber = (TextView) convertView.findViewById(R.id.bankcard_number);
            holder.mBankHead = (ImageView) convertView.findViewById(R.id.bank_head);
            holder.mBankname = (TextView) convertView.findViewById(R.id.bankname);
            holder.mCardMaster = (TextView) convertView.findViewById(R.id.card_master);
            holder.mBankIcon = (RelativeLayout) convertView.findViewById(R.id.bank_icon);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //数据绑定
        MyBankCardBean.RowsBean rowsBean = mDataSets.get(position);
        String bcCardnum = rowsBean.getBcCardnum();
        int length = bcCardnum.length();
        holder.mBankcardNumber.setText("**** ****  ****  "+ bcCardnum.substring(length - 4, length));
        holder.mBankname.setText(rowsBean.getBankCode().getBcNote());
        holder.mCardMaster.setText(rowsBean.getBcName());
        //需要变化的三个量:一个是背景  一个是图案  一个是颜色
        int indexOf = banklists.indexOf(rowsBean.getBankCode().getBcCode());
        if (indexOf == -1) {
            holder.mBankIcon.setBackgroundResource(backgrounds[backgrounds.length - 1]);
            holder.mBankHead.setImageResource(images[images.length - 1]);
            holder.mCardview.setBackgroundResource(backgroundColor[backgroundColor.length - 1]);
        } else {
            holder.mBankIcon.setBackgroundResource(backgrounds[indexOf]);
            holder.mBankHead.setImageResource(images[indexOf]);
            holder.mCardview.setBackgroundResource(backgroundColor[indexOf]);
        }
        return convertView;
    }


    class ViewHolder {
        ImageView mBankHead;
        TextView mBankname;
        TextView mCardMaster;
        TextView mBankcardNumber;
        RelativeLayout mBankIcon;
        CardView mCardview;


    }
}